package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.T_dict_child;
import model.T_dict_parent;
import model.V_dict;

import com.bean.Attributes;
import com.bean.TreeBean;
import com.et.ar.exception.ActiveRecordException;
import com.et.mvc.JsonView;
import com.wzzz.cache.CacheHandler;

public class DicManagerController extends ApplicationController {
	/*
	 * 获得左侧树状列表
	 */
	public JsonView getParentDic() {
		List<T_dict_parent> parents = CacheHandler.getDicCacheObjectByKey();
		List<TreeBean> list = new ArrayList<TreeBean>();
		for (T_dict_parent t : parents) {
			TreeBean tb = new TreeBean();
			tb.setId(t.getP_code());
			tb.setText(t.getP_name());
			tb.setIconCls("icon-ok");
			Attributes a = new Attributes();
			a.setUrl("/dicManager/");
			tb.setAttributes(a);
			list.add(tb);
		}
		// Map<String, Object> result = new HashMap<String, Object>();
		// result.put("list", list);
		return new JsonView(list);
	}
	
	/*
	 * 查找子字典的数据
	 */
	public JsonView getDics(String id, int rows, int page) {
		List<V_dict> list = CacheHandler.getCacheListByKey(id);
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Object> lst = pageList(list,rows,page);
		result.put("total", list.size());
		result.put("rows", lst);
		return new JsonView(result);
	}
	
	/*
	 * 获得一个具体的子字典信息
	 */
	public JsonView getDic(String c_code, String p_code)
			throws ActiveRecordException {
		List<V_dict> vds = V_dict.findBySql(V_dict.class,
				"select * from v_dict where p_code=? and c_code=?",
				new Object[] { p_code, c_code });
		return new JsonView(vds.get(0));
	}
	
	/*
	 * 修改字典数据
	 */
	public JsonView updateDic(String c_seq) throws Exception {
		T_dict_child vd = T_dict_child.find(T_dict_child.class, c_seq);
		updateModel(vd);
		vd.save();
		CacheHandler.reloadDicChildren(vd.getP_code());
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	
	/*
	 * 新增字典操作
	 */
	public JsonView saveDic(T_dict_child t_dict_child) throws Exception {
		t_dict_child.save();
		CacheHandler.reloadDicChildren(t_dict_child.getP_code());
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	/*
	 * 更改字典状态
	 */
	public JsonView changeActive(String c_seq) throws Exception {
		T_dict_child vd = T_dict_child.find(T_dict_child.class, c_seq);
		String isActive = vd.getIs_active();
		if("0".equals(isActive)){
			isActive = "1";
		}else{
			isActive = "0";
		}
		vd.setIs_active(isActive);
		vd.update();
		CacheHandler.reloadDicChildren(vd.getP_code());
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	/*
	 * 删除子字典数据
	 */
	public JsonView cancelDic(String c_seq,String p_code) throws Exception {
		String conds = " c_seq in ("+c_seq+")";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		T_dict_child.deleteAll(T_dict_child.class, conds, args);

		CacheHandler.reloadDicChildren(p_code);
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	
	public JsonView searchDic(String p_code,String c_code,String c_name,int rows, int page) throws Exception{
		c_code = new String(c_code.getBytes("ISO-8859-1"),"utf-8");
		c_name = new String(c_name.getBytes("ISO-8859-1"),"utf-8");
//		List<V_dict> list = CacheHandler.getCacheListByKey(p_code);
		StringBuilder sb = new StringBuilder("select * from v_dict where p_code= "+p_code );
		if(c_code != null){
			sb.append(" and c_code like '%").append(c_code).append("%' ");
		}
		if(c_name != null){
			sb.append(" and c_name like '%").append(c_name).append("%' ");
		}
		List<V_dict> vds = V_dict.findBySql(V_dict.class,
				sb.toString(),	new Object[] { });
	
		Map<String, Object> result = new HashMap<String, Object>();
		List<Object> lst = pageList(vds,rows,page);
		result.put("success",true);
		result.put("total", vds.size());
		result.put("rows", lst);
		return new JsonView(result);
		
	}
	
	public JsonView getChildListByPcode(String p_code) throws Exception{
		
		List<V_dict> vdictList = CacheHandler.getCacheListByKey(p_code);
		return new JsonView(vdictList);
	}
	/*
	 * 字典类分页方法
	 */
//	public List<Object> pageList(List<V_dict> list,int rows,int pages){
//		List<Object> lst = new ArrayList(rows);		
//		int startPos = 0;
//		int endPos = 0;
//		//判读是不是第一页
//		if(pages ==1 ){
//			startPos = 0;
//			endPos = pages * rows;
//		}
//		//如果不是第一页
//		if(pages > 1){
//			startPos = (pages-1) * rows +1;
//			endPos = pages * (rows);
//		}
//		//如果结束游标大于总记录数
//		if(endPos>list.size()){
//			endPos = list.size();
//		}
//		
//		if(list!=null && list.size()>0){
//			for(int i=startPos;i<endPos;i++){
//				Object obj = list.get(i);
//				lst.add(obj);
//			}
//		}
//		return lst;
//	}
}
